package com.api.zszsj2.ZYT.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import com.mzlion.core.date.DateUtils;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 用于查询n个工作日（包含法定节假日、休息日、调休）后的日期
 */
@Log4j
public class HolidayUtils {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 用于判断n个工作日(排除节假日、周六日包含节后补班数据)后的日期
     *
     * @param list 节假日数据源
     * @param weekDayList 节后补班数据源
     * @param restDays 一周当中的休息日
     * @param today  计算开始时间
     * @param num  多少个工作日 根据需要自行安排
     * @return
     * @throws ParseException
     */
    public static String getScheduleActiveDate(List<String> list, List<String> weekDayList, List<Integer> restDays, String today,int num) throws ParseException {
        Date date = DateUtil.parseDate(today);
        Date tomorrow = null;
        int delay = 1;
        while(delay <= num){
            tomorrow = getTomorrow(date);
            String test = parseDate(tomorrow, "yyyy-MM-dd");
            //当前日期+1即tomorrow,判断是否是节假日,同时要判断是否是周末,都不是则将scheduleActiveDate日期+1,直到循环num次即可------不是节假日不是周末并且不是补班
            if((!isWeekend(sdf.format(tomorrow), restDays) && !isHoliday(sdf.format(tomorrow),list))  || isWorkWeekDay(sdf.format(tomorrow),weekDayList)){
                if(isWorkWeekDay(sdf.format(tomorrow),weekDayList)){
                    log.info(sdf.format(tomorrow) + "::是节假日调休补班");
                }else{
                    log.info(sdf.format(tomorrow) + "::是正常工作日");
                }
                delay++;
                date = tomorrow;
            }else if (isHoliday(sdf.format(tomorrow),list)){
//                tomorrow = getTomorrow(tomorrow);
                date = tomorrow;
                log.info(sdf.format(tomorrow) + "::是节假日");
            }else if(isWeekend(sdf.format(tomorrow), restDays)){//是周六日并且不是节后补班
                if(!isWorkWeekDay(sdf.format(tomorrow),weekDayList)){
                    date = tomorrow;
                    log.info(sdf.format(tomorrow) + "::是休息日");
                }

            }
        }
        log.info(today+"后"+num+"个工作日后,日期为::" + sdf.format(date));
        return parseDate(date, "yyyy-MM-dd");
    }

    /**
     * 获取明天的日期
     * @param date
     * @return
     */
    public static Date getTomorrow(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1);
        date = calendar.getTime();
        return date;
    }

    /**
     * 判断是否是weekend
     * @param sdate
     * @return
     * @throws ParseException
     */
    public static boolean isWeekend(String sdate, List<Integer> restDays) throws ParseException {
        int week = DateTime.of(sdate, "yyyy-MM-dd").dayOfWeekEnum().getValue();
        week = week == 1 ? 7 : week -1;
        Date date = sdf.parse(sdate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        for (Integer restday : restDays){
            if(week  == (restday + 1)){
                return true;
            }
        }
        return false;
//        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
//            return true;
//        } else{
//            return false;
//        }
    }

    /**
     * 判断是否是holiday
     * @param sdate
     * @param list
     * @return
     * @throws ParseException
     */
    public static boolean isHoliday(String sdate, List<String> list) throws ParseException {
        if(list.size() > 0){
            for(int i = 0; i < list.size(); i++){
                if(sdate.equals(list.get(i))){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断是否是补班
     * @param sdate
     * @param list
     * @return
     * @throws ParseException
     */
    public static boolean isWorkWeekDay(String sdate, List<String> list) throws ParseException {
        if(list.size() > 0){
            for(int i = 0; i < list.size(); i++){
                if(sdate.equals(list.get(i))){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 把日期格式化成字符串
     * @param date
     * @param format 例: yyyy-MM-dd
     * @return
     */
    public static String parseDate(Date date, String format) {
        SimpleDateFormat formater = new SimpleDateFormat(format);
        String dateString;
        dateString = formater.format(date);
        return dateString;
    }
}